/**
 * create by young (young2f@outlook.com) on 2020/9/12
 */


'use strict';
import React from 'react';
import {connect} from 'dva/index';
import {Button, Calendar, Form, message, Upload} from 'antd';
import styles from '../index.less';
import DirTree from '../../components/DirTree';
import FormBase from '../../components/Base/FormBase';
import notify from "../../components/NotificationExtra";
import moment from "moment";
import {UploadOutlined} from "@ant-design/icons";

const _ = require('lodash');

const table = 'Dict';

@Form.create()
@connect(({
            user,
            global,
            dynamicLabel,
            material,
            treeNode
          }) => ({...user, ...global, ...material, ...dynamicLabel, ...treeNode}))
export default class Index extends FormBase {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    const {dispatch} = this.props;
    dispatch && dispatch({
      type: 'global/getModel',
      payload: {table, tenantCode: this.getTenantCode()},
    });

    const {treeNode} = this.state;
    if (!treeNode) return;
    const {treeId} = treeNode;
    this.props.dispatch({
      type: 'material/findMany',
      payload: {treeId},
    });

  }


  onTreeNodeSelected = (node) => {
    if (!node) return;
    this.state.treeNode = node;
    this.componentDidMount();
  };
//todo
  onTreeDelete = (_id, result) => {
    // const loginInfo = wStore.getStore('loginInfo');
    // const {tenantCode} = loginInfo;
    // const where = {organizationCode: _id, tenantCode};
    // if (result.status === 'ok') {
    //   this.props.dispatch({
    //     type: 'device/updateDevice',
    //     payload: {table: 'device', where, data: {organizationCode: ''}},
    //   });
    // }
  };


  onPanelChange = (value, mode) => {
    console.log(value, mode);
  }
  dataPage = () => {
    const props = {
      name: 'file', // 文件接收的字段名
      action: 'http://127.0.0.1:18008/api/global/upload', // 这里替换为你的上传接口地址
      headers: {
        authorization: 'authorization-text',
      },
      onChange(info) {
        const { status } = info.file;
        if (status !== 'uploading') {
          console.log(info.file, info.fileList);
        }
        if (status === 'done') {
          message.success(`${info.file.name} file uploaded successfully`);
        } else if (status === 'error') {
          message.error(`${info.file.name} file upload failed.`);
        }
      },
      // 限制文件类型为PDF
      beforeUpload: file => {
        const isPdf = file.type === 'application/pdf';
        if (!isPdf) {
          message.error('You can only upload PDF files!');
        }
        return isPdf;
      },
    };
    return <Form>
      <Upload {...props}>
        <Button icon={<UploadOutlined />}>Click to Upload</Button>
      </Upload>
    </Form>


    // const { treeNode, previewVisible, previewImage, fileList} = this.state;
    // if (!treeNode) return;
    // const uploadPicButton = (
    //   <div>
    //     <Icon type="plus"/>
    //     <div className="ant-upload-text">上传图片</div>
    //   </div>
    // );
    // return (
    //   <div style={{marginTop: 0}}>
    //     <ViewTitle
    //       title={'列表'}
    //       level={1}
    //       ></ViewTitle>
    //     {
    //       <div style={{marginLeft: '64px'}}>
    //         <Upload
    //           action={'http://127.0.0.1:28900/api/global/upload?type=inboundLot'} // 替换为你的上传API地址
    //           listType="picture-card"
    //           fileList={fileList}
    //           accept={".pdf,.jpg"}
    //           onPreview={this.handlePreview}
    //           onChange={this.handleChange}
    //           onDownload={file => {
    //             console.log('ondownlad', file);
    //           }}
    //           beforeUpload={(file, fileList) => {
    //             const isLt2M = file.size / 1024 / 1024 < 2;
    //             if (!isLt2M) {
    //               notify({type: 'error', message: '文件过大！'});
    //               return isLt2M;
    //             }
    //             return false;
    //           }}
    //         >
    //           {uploadPicButton}
    //         </Upload>
    //         <Modal visible={previewVisible} footer={null} onCancel={this.handleCancel}>
    //           <img alt="example" style={{width: '100%'}} src={previewImage}/>
    //         </Modal>
    //       </div>
    //     }
    //   </div>
    // );
  };

  render() {
    const {dispatch, expandedKeys, _notify} = this.props;
    console.log('_notify', _notify)
    if (_notify) {
      notify(_notify);
    }
    let title = '文档分类';
    return (
      <Form className={styles.content}>
        <DirTree
          table={table}
          dispatch={dispatch}
          onTreeDelete={this.onTreeDelete}
          onTreeNodeSelected={this.onTreeNodeSelected}
          expandedKeys={expandedKeys}
          defaultExpandAll={true}
          title={title}
        > {this.dataPage()}</DirTree>

      </Form>
    );
  }
}
